// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import AudioManager from "../manager/AudioManager";
import DataManager from "../manager/DataManager";
import SdkManager from "../manager/SdkManager";
import { StaticInstance } from "../StaticInstance";
import { random } from "../Utils";
import BaseLayer from "./Baselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainPkLayer extends BaseLayer {

    btnPause: cc.Node = null
    player1: cc.Node = null
    player2: cc.Node = null

    onLoad() {
        this.btnPause = this.node.getChildByName('btn_pause')
        this.btnPause.on('click', this.onPauseClick, this)
        this.player1 = cc.find('pk_bar/players/player1', this.node)
        this.player2 = cc.find('pk_bar/players/player2', this.node)
    }

    onDestroy() {
        this.btnPause.off('click', this.onPauseClick, this)
    }

    onEnable() {
        this.rendorPowerTimer()
        SdkManager.instance.toggleCustomRowAd(true)
    }

    onDisable() {
        SdkManager.instance.toggleCustomRowAd(false)
    }

    onPauseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.EXIT_PK)
    }

    setPlayerNum(isInit: boolean) {
        const nickname1 = this.player1.getChildByName('nickname')
        const nums1 = this.player1.getChildByName('nums')
        const nickname2 = this.player2.getChildByName('nickname')
        const nums2 = this.player2.getChildByName('nums')
        if (isInit) {
            nickname1.getComponent(cc.Label).string = '你'
            nickname2.getComponent(cc.Label).string = `${DataManager.instance.currentAI.nickName}`
            nums1.getComponent(cc.Label).string = `剩余${DataManager.instance.levelPlayerTotal}人`
            nums2.getComponent(cc.Label).string = `剩余${DataManager.instance.levelPlayerTotal}人`
        } else {
            nums1.getComponent(cc.Label).string = `剩余${DataManager.instance.levelPlayerTotal - DataManager.instance.levelPlayerClear}人`
        }
    }

    setAI(bool: boolean) {
        if (bool) {
            this.unscheduleAllCallbacks()
            // 难度取决胜率
            let fate = 0
            let inter = 1
            let rand = 5
            if (DataManager.instance.countWin) {
                fate = (DataManager.instance.countWin / (DataManager.instance.countWin + DataManager.instance.countLose)) * 100
            }
            if (DataManager.instance.countWin >= 5) {
                if (fate >= 90) {
                    inter = 0.1
                    rand = 3
                }
                if (fate >= 80) {
                    inter = 0.5
                    rand = 4
                }
                if (fate <= 30) {
                    inter = 1
                    rand = 5
                }
            }
            // console.log('难度系数', inter, rand)
            this.schedule(() => {
                const r = random(1, 10)
                if (r >= rand) {
                    DataManager.instance.levelAIClear += 1
                    const nums2 = this.player2.getChildByName('nums')
                    nums2.getComponent(cc.Label).string = `剩余${DataManager.instance.levelPlayerTotal - DataManager.instance.levelAIClear}人`
                    if (DataManager.instance.levelAIClear >= DataManager.instance.levelPlayerTotal) {
                        this.unscheduleAllCallbacks()
                        StaticInstance.gameManager.onGameOverPk(false)
                    }
                }
            }, inter)
        } else {
            this.unscheduleAllCallbacks()
        }
    }
}
